import { Controller, Get, Post, Body, Patch, Param, Res } from '@nestjs/common';
import { EcfoodService } from './ecfood.service';

const quarter = (month: number) => {
  if (month === 1 || month === 2 || month === 3) {
    return 'Q1'
  }
  if (month === 4 || month === 5 || month === 6) {
    return 'Q2'
  }
  if (month === 7 || month === 8 || month === 9) {
    return 'Q3'
  }
  if (month === 10 || month === 11 || month === 12) {
    return 'Q4'
  }
  return 'Q1'
}
@Controller('ecfood')
export class EcfoodController {
  constructor(private readonly ecfoodService: EcfoodService) { }


  @Get('/list/:year')
  async getdata(@Param() param, @Res() res) {
    const result = await this.ecfoodService.getdata(Number(param.year))

    res.status(200).send({
      data: result,
      message: "成功"
    })
  }
  @Get('/:type')
  async creat(@Param('type') param, @Res() res) {
    const data = new Date(Date.now())
    const year = data.getFullYear()
    const month = data.getMonth() + 1
    const q = quarter(month)
    const result2 = await this.ecfoodService.findfood(year, q, param)
    if (!result2) {
      await this.ecfoodService.creat(year, q, param)
      res.status(200).send({
        mesage: "ok"
      })
    } else {
      res.status(200).send({
        mesage: "ok"
      })
    }
  }

}
